package com.dmz.spring.security.controller;
import java.io.IOException;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dmz.spring.security.utils.VerifyCodeUtil;


@RestController
public class LoginController {
	
	@GetMapping("/getCode")
    public void getVerifyCode(HttpServletRequest request,HttpServletResponse response){
        /* 生成验证码字符串 */
        String verifyCode = VerifyCodeUtil.generateTextCode(VerifyCodeUtil.TYPE_ALL_MIXED, 4, null);
        String uuid = UUID.randomUUID().toString();
        System.out.println("verifyCode:"+verifyCode);
        System.out.println("uuid:"+uuid);
        request.getSession().setAttribute(uuid,verifyCode);
        int w = 111, h = 36;
        HttpSession session = request.getSession();
        session.setAttribute("verifyCode", verifyCode);
        try {
			ImageIO.write(VerifyCodeUtil.generateImageCode(verifyCode, w, h,h, false, null, null, null), "JPEG", response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

}
